package com.hst.capacity.infra.web.config;


import com.hst.capacity.application.quartz.JobFactory;
import com.hst.capacity.application.quartz.VirtualAcceptOrderQuartz;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
@EnableScheduling
public class QuartzConfiguration {

    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean() {
        JobDetailFactoryBean bean = new JobDetailFactoryBean();
        bean.setJobClass(VirtualAcceptOrderQuartz.class);
        return bean;
    }

    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean bean) {
        CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean();
        factoryBean.setJobDetail(bean.getObject());
        factoryBean.setCronExpression("0 0/10 * * * ?");
//        factoryBean.setCronExpression("0 1 5 * * ?");
        return factoryBean;
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean bean, JobFactory jobFactory) {
        SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
        factoryBean.setTriggers(bean.getObject());
        factoryBean.setJobFactory(jobFactory);
        return factoryBean;
    }

}
